package dao;

import db.Connect;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 07.10.13
 * Time: 14:06
 * To change this template use File | Settings | File Templates.
 */
public class LoginDao {
    private static final String loginSQL =
            "SELECT * FROM USERS WHERE USER_LOGIN = ? AND USER_PASSWORD = ? ";

    private static final String regSQL="insert into USERS values (TT_SEQ.nextVal,?,?,?,?,3)";
    private static final Logger log = Logger.getLogger(LoginDao.class);
    public boolean checkUser(String login, String pass) {
        try {
            PreparedStatement st = null;
            ResultSet rs = null;
            Connection cn = null;
            try {


                cn = Connect.getConnect();

                try {

                    st = cn.prepareStatement(loginSQL);

                    st.setString(1, login);
                    st.setString(2, pass);
                    //     System.out.println(st.toString());
                    try {
                        rs = st.executeQuery();
                        //       System.out.println(rs.toString());
                        return rs.next();


                    } finally {

                        rs.close();
                    }


                } finally {
                    st.close();
                }

            } finally {
                cn.close();

            }
        } catch (SQLException e) {
            System.out.print(e);
           log.error(e);
        }
        return false;
    }

    public boolean inserUser(String login, String pass, String name, String email) {
        try {
            PreparedStatement st = null;
            ResultSet rs = null;
            Connection cn = null;
            try {


                cn = Connect.getConnect();

                try {

                    st = cn.prepareStatement(regSQL);

                    st.setString(1, login);
                    st.setString(2, pass);
                    st.setString(3, name);
                    st.setString(4, email);
                    //     System.out.println(st.toString());
                    try {
                        rs = st.executeQuery();
                        //       System.out.println(rs.toString());
                        return rs.next();


                    } finally {

                        rs.close();
                    }


                } finally {
                    st.close();
                }

            } finally {
                cn.close();

            }
        } catch (SQLException e) {
            System.out.print(e);
            log.error(e);
        }
        return false;
    }

}
